package org.ielts.service;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.base.Teaching;
import org.ielts.domain.report.TeachingWithCount;
import org.ielts.profile.IeltsProfile;
import org.ielts.repository.TeachingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;

@Service
public class TeachingService {

	@Autowired
	TeachingRepository teachingRepository;

	@Transactional
	public Iterable<Teaching> findBetweenDate(Date startDate, Date endDate) {

		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (StringUtils.isBlank(campusId)) {
			Iterable<Teaching> teachers = teachingRepository.findBetweenDate(startDate, endDate);
			return teachers;
		} else {
			Iterable<Teaching> teachers = teachingRepository.findBetweenDate(startDate, endDate, campusId);
			return teachers;
		}
	}

	@Transactional
	public Iterable<Teaching> findBetweenDateAndTeacher(Date startDate, Date endDate, String teacherId) {
		Iterable<Teaching> teachings = teachingRepository.findBetweenDateAndTeacher(startDate, endDate, teacherId);

		return teachings;
	}

	@Transactional
	public Iterable<TeachingWithCount> findReportBetweenDateAndTeacher(Date startDate, Date endDate, String teacherId) {
		// TODO 分开校区和全校
		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (StringUtils.isBlank(campusId)) {
			Iterable<TeachingWithCount> reports = teachingRepository.findReportBetweenDateAndTeacher(startDate,
					endDate, teacherId);
			return reports;
		} else {
			Iterable<TeachingWithCount> reports = teachingRepository.findReportBetweenDateAndTeacherAndCampus(
					startDate, endDate, teacherId, campusId);
			return reports;
		}
	}
}
